package mx.com.mindbits.bandpay;

import java.util.concurrent.ExecutionException;

import mx.com.mindbits.bandpay.asynctask.CardDeviceTask;
import mx.com.mindbits.bandpay.asynctask.PayServicesTask;
import mx.com.mindbits.bandpay.business.TransactionBusiness;
import mx.com.mindbits.bandpay.utils.CardDataHelp;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.bandpay.utils.CustomToastNotification;
import mx.com.mindbits.bandpay.utils.GPSTracker;
import mx.com.mindbits.bandpay.utils.GenericAlert;
import mx.com.mindbits.bandpay.utils.USBReceiver;
import mx.com.mindbits.bandpay.utils.Utils;
import mx.com.mindbits.bandpay.utils.Validations;
import mx.com.mindbits.bandpay.utils.cardDialog;
import mx.com.mindbits.bandpay.utils.cardDialog.DialogListener;
import mx.com.mindbits.bandpay.vo.TAECompraVO;
import mx.com.mindbits.bridge.Blowfish;
import mx.com.mindbits.emvcardreader.vo.EMVdata;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.acs.smartcard.ReaderException;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.google.gson.Gson;

public class CardDataCaptureActivity extends SherlockActivity {

	private Bundle receiver;
	private UsbManager managerUSB;
	private AlertDialog.Builder detalleTransaccion;	
	private ProgressDialog progressRead;	
	private TextView cardNumber;
	private TextView expiration;
	private TextView holderName;
	private TextView cvvLabel;
	private TextView barraEstado;
	private EditText cvv2Number;	
	private PendingIntent pendingIntent;
	private IntentFilter filter;
	private IntentFilter responseReceiverFilter;
	private Menu menu;
	private MenuItem item; 
	private Context contexto = this;	
	//
	private TextView fechaHora;
	private TextView tarjeta;
	private TextView titular;
	private TextView monto;
	private TextView servicio;
	private TextView numeroTAE;
	private TextView compania;
	private TextView labelTAE;
	private TextView labelCompania;
	//
	private TAECompraVO compraReceived; 
	private GPSTracker gps;
	private String arqc;	
	private String aid;
	private USBReceiver usbReceiver;
	private EMVdata dataEMV;
	private CardDeviceTask pickDataCard;
	
	private Button buttonSend;
	private Button buttonCancel;
	private Button buttonOpenAgain;
	private boolean isTae = false;
	
	private CardDataHelp cardDataHelp;
	private AlertDialog dialogBuilder;
	private cardDialog  card;
	private ActionBar actionBar;
	private boolean flagDialog2; 
	
	private TransactionBusiness transactionBusiness;
	private Gson jsonParser = new Gson();//not implemented
	
	private BroadcastReceiver intentResponseReceiver = new BroadcastReceiver(){
		
		@Override
		public void onReceive(Context context, Intent intent) {			
			String mensaje = intent.getExtras().getString(ConstantsValues.ACTION_ATTACHED);
			barraEstado = (TextView) findViewById(R.id.estatus_bar);
			barraEstado.setText(mensaje);	
			
			
			if(mensaje.equals(contexto.getString(R.string.label_statusbar_attached))){
					settingRequestUsbService();
				
			}else if(mensaje.equals(contexto.getString(R.string.label_statusbar_opened))){
				
				Log.d(ConstantsValues.TAG_READER, "INTENT RESPONSE RECEIVER DIPOSITIVO ABIERTO");
				
				dialogBuilder = cardDataHelp.insertDeviceHelp(contexto.getString(R.string.label_paso_2),
						contexto.getString(R.string.label_msn_paso_2),
						R.drawable.tarjetaconnected_tiny);
				dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok), new OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {						
						settingReadDevice();
						flagDialog2 = false;
					}					
				});				
				dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, 
						context.getString(android.R.string.cancel), new OnClickListener(){
					@Override
					public void onClick(DialogInterface dilog, int i){
						flagDialog2 = false;
					}
				});
				if(!flagDialog2){
					flagDialog2 = true;
					dialogBuilder.show();
					
				}				
				
			}else if(mensaje.equals(context.getString(R.string.label_statusbar_readBad))){
				
				Log.d(ConstantsValues.TAG_READER, "INTENT RESPONSE RECEIVER LECTURA MALA");
				dialogBuilder = cardDataHelp.insertDeviceHelp(contexto.getString(R.string.label_paso_2),
						contexto.getString(R.string.label_statusbar_readBad),
						R.drawable.tarjetaconnected_tiny);
				dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE, contexto.getString(android.R.string.ok),
						new OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {						
						settingReadDevice();
						flagDialog2 = false;
					}					
				});				
				dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE,
						contexto.getString(android.R.string.cancel), new OnClickListener(){
					@Override
					public void onClick(DialogInterface dilog, int i){
						flagDialog2 = false;
					}
				});
				if(!flagDialog2){
					flagDialog2 = true;
					dialogBuilder.show();
					
				}

			}else if(mensaje.equals(context.getString(R.string.label_statusbar_read))){
				//successfully read card
							
						Log.d(ConstantsValues.TAG_READER, "INTENT RESPONSE RECEIVER LISTO PARA LEER");
						card = new cardDialog(contexto, contexto.getString(R.string.label_paso_3),
								contexto.getString(R.string.label_msn_paso_3));
						card.setCancelable(false);
						card.requestWindowFeature(Window.FEATURE_NO_TITLE);
						card.setDialogResult(new DialogListener(){
							@Override
							public void finish(String CVV) {
								if(CVV.length() == 3){
									cvv2Number.setText(CVV);
									settingSendSell();
								}else{
									cvv2Number.setText(null);
							
								}
							}
						});
						card.show();
						
			}else if(mensaje.equals(context.getString(R.string.label_statusbar_detached))){
								
			}
		}		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card_data_capture);
		
		receiver = this.getIntent().getExtras();
		gps = new GPSTracker(contexto);
		gps.getLocation();
		flagDialog2 = false;
		actionBar = getSupportActionBar();
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		compraReceived = new TAECompraVO();
				
		cardNumber  = (TextView) findViewById(R.id.cardNumber);
		expiration  = (TextView) findViewById(R.id.expirationDate);
		holderName  = (TextView) findViewById(R.id.holderName);
		cvvLabel	= (TextView) findViewById(R.id.cvvLabel);
		cvv2Number  = (EditText) findViewById(R.id.cvv2Capture);
		
		barraEstado = (TextView) findViewById(R.id.estatus_bar);
		buttonOpenAgain = (Button) findViewById(R.id.button_open_again);
		buttonOpenAgain.setText(R.string.label_button_read_new_card);
		buttonOpenAgain.setBackgroundResource(R.drawable.blue_button_shape);
		
		fechaHora = (TextView)findViewById(R.id.textDate);
		tarjeta   = (TextView)findViewById(R.id.textNCardConf);
		titular   = (TextView)findViewById(R.id.textHolderNConf);
		monto     = (TextView)findViewById(R.id.textMountNConf);
		servicio  = (TextView)findViewById(R.id.textServiceNConf);		
		labelTAE  = (TextView)findViewById(R.id.textTAEConf);
		labelCompania = (TextView)findViewById(R.id.textCompanyConf);
		numeroTAE = (TextView)findViewById(R.id.textTAENConf);
		compania  = (TextView)findViewById(R.id.textCompanyNConf);
		
		cardDataHelp = new CardDataHelp(this.contexto);
		
		if(ConstantsValues.DEVICE_ALREADY_ATTACHED){
			barraEstado.setText(contexto.getString(R.string.label_statusbar_attached));
			dialogBuilder = cardDataHelp.insertDeviceHelp(contexto.getString(R.string.label_paso_1a),
					contexto.getString(R.string.label_msn_paso_1a),
					R.drawable.cardreader_tiny);
			dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE,
					contexto.getString(android.R.string.ok), new OnClickListener(){

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							settingRequestUsbService();
						}				
			});
			dialogBuilder.show();
		}else{			
			ConstantsValues.REQUEST_SENDED = true;
			
			
			dialogBuilder = cardDataHelp.insertDeviceHelp(contexto.getString(R.string.label_paso_1),
					contexto.getString(R.string.label_msm_paso_1),
					R.drawable.cardreader_tiny);
			dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE,
					contexto.getString(android.R.string.ok), new OnClickListener(){
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					ConstantsValues.REQUEST_SENDED = false;
					/////////////////fix////////////////////////
					try {
						Utils.startDeviceWithoutAttach(contexto);
					} catch (Exception e) {
						e.printStackTrace();
					}
					///////////////////////////////////////////
					settingRequestUsbService();
				}
			});
			
			dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE,
					contexto.getString(android.R.string.cancel), new OnClickListener(){
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			
		dialogBuilder.show();
		
		}
		
		cvv2Number.setEnabled(false);
						
		/////////////////////Intent filter//////////////////////////
		usbReceiver = new USBReceiver();
		pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent ("com.android.example.USB_PERMISSION"), 0);
		filter = new IntentFilter(ConstantsValues.ACTION_USB_PERMISION);
		responseReceiverFilter = new IntentFilter();
		responseReceiverFilter.addAction(ConstantsValues.ACTION_USED);
		registerReceiver(usbReceiver,filter);		
				
		
		////////////////////////Recepcion de extras////////////////////7
		if(receiver != null){
			compraReceived = (TAECompraVO) receiver.getSerializable("TAEtrans");
			compraReceived.setCx(String.valueOf(gps.getLatitude()));
			compraReceived.setCy(String.valueOf(gps.getLongitude()));
			ConstantsValues.MOUNT_PURCHASE = compraReceived.getMonto();
			Log.d(ConstantsValues.TAG_READER, "COORDENADAS: ["+compraReceived.getCx()+"|"+compraReceived.getCy()+"]" +
			"Monto : " + compraReceived.getMonto());
			if (!compraReceived.getNumbertae().isEmpty()){
				isTae = true;
			}
		}
		
		receiver.putSerializable("compraComplete", compraReceived);
		
		//////////////////Cuadros de dialog///////////////////////////			
		progressRead = new ProgressDialog(contexto);
		progressRead.setTitle(R.string.progress_dialog_title1);		
		progressRead.setMessage(this.getString(R.string.progress_dialog_body1));
		progressRead.setCancelable(false);
		progressRead.setCanceledOnTouchOutside(false);
		
		cvvLabel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				GenericAlert.generateInformativeDialog(contexto.getString(R.string.app_name), 
						contexto.getString(R.string.alert_message_3), R.drawable.cvv_sombra, contexto).show();
				
			}
		});
		
		detalleTransaccion = new AlertDialog.Builder(contexto);
		detalleTransaccion.create();
		detalleTransaccion.setTitle(R.string.statusDialog_title);
		
		buttonOpenAgain.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					ConstantsValues.REQUEST_SENDED = true;
					dialogBuilder = cardDataHelp.insertDeviceHelp(contexto.getString(R.string.label_paso_1),
							contexto.getString(R.string.label_msm_paso_1),
							R.drawable.cardreader_tiny);
					dialogBuilder.setButton(DialogInterface.BUTTON_POSITIVE,
							contexto.getString(android.R.string.ok), new OnClickListener(){
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							ConstantsValues.REQUEST_SENDED = false;
							/////////////////fix////////////////////////
							try {
								Utils.startDeviceWithoutAttach(contexto);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							///////////////////////////////////////////
							settingRequestUsbService();
						}
					});
					
					dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE,
							contexto.getString(android.R.string.cancel), new OnClickListener(){
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
						}
					});
					
				dialogBuilder.show();
					
				}
			});
			
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		this.menu = menu;
		getSupportMenuInflater().inflate(R.menu.activity_card_data_capture, menu);
			item = (MenuItem) menu.findItem(R.id.action_cancel);
			buttonCancel =(Button) item.getActionView().findViewById(R.id.buttonGeneric);
			buttonCancel.setText("Cancelar");
			buttonCancel.setBackgroundResource(R.drawable.blue_button_shape);
			buttonCancel.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					ConstantsValues.REQUEST_SENDED  = false;
					Intent backMenu = new Intent(contexto, MainMenuActivity.class);
					startActivity(backMenu);
				}
			});
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
			case android.R.id.home:
			Intent intent = new Intent(this, MainMenuActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
				break;
		}
		return false;
	}
		
	@Override
	public void onPause(){
		unregisterReceiver(intentResponseReceiver);
		super.onPause();
		Log.d(ConstantsValues.TAG_READER, "ENTRE EN PAUSA");
		ConstantsValues.DEVICE_ALREADY_ATTACHED = false;
		barraEstado = (TextView) findViewById(R.id.estatus_bar);
		barraEstado.setText(contexto.getString(R.string.label_statusbar_attached));
		if(dialogBuilder != null)
			dialogBuilder.dismiss();
		if(ConstantsValues.tarjeta != null){
			if (ConstantsValues.tarjeta.getReader().isOpened()){
				try {		
					ConstantsValues.tarjeta.powerDown();
					ConstantsValues.tarjeta.closeDevice();
					ConstantsValues.WAS_ON_PAUSE = true;
					ConstantsValues.REQUEST_SENDED = false;
					ConstantsValues.DEVICE_ALREADY_ATTACHED = false;
					Log.d(ConstantsValues.TAG_READER, "VALOR ["+ConstantsValues.WAS_ON_PAUSE+"]");
				} catch (ReaderException e) {
					Log.e(ConstantsValues.TAG_READER,"ERROR: "+e);			
				}		 
				if (ConstantsValues.tarjeta.getReader().isOpened()){
					Log.d(ConstantsValues.TAG_READER, "USB ya inicializado...");			
				}
					Log.d(ConstantsValues.TAG_READER, "Entra on stop...");
			}
		}		
	}// end pause
	
	
	@Override
	public void onResume(){
		registerReceiver(intentResponseReceiver,responseReceiverFilter);
		Log.d(ConstantsValues.TAG_READER, "ON RESUME");
		super.onResume();		
		if(ConstantsValues.REQUEST_SENDED){
			barraEstado.setText(R.string.label_statusbar_opened);			
		}
		if(ConstantsValues.WAS_ON_PAUSE){			
			GenericAlert.generateAlertDialog(this.getString(R.string.app_name),
					this.getString(R.string.alert_text_10),this).show();
			ConstantsValues.WAS_ON_PAUSE = false;
		}				
	}
	
	public void settingRequestUsbService(){
		Log.i(ConstantsValues.TAG_READER, "<<" + ConstantsValues.dispositivo + ">>" +  ConstantsValues.REQUEST_SENDED);
		if(ConstantsValues.dispositivo != null){
			if(!ConstantsValues.REQUEST_SENDED){
				managerUSB = (UsbManager)contexto.getSystemService(Context.USB_SERVICE);
				managerUSB.requestPermission(ConstantsValues.dispositivo, pendingIntent);
				
			}																	
		}else{
			GenericAlert.generateAlertDialog(contexto.getString(R.string.app_name),
					contexto.getString(R.string.alert_text_9) , contexto).show();
		}
	}

	public void settingReadDevice(){
		if(ConstantsValues.tarjeta != null && ConstantsValues.tarjeta.getReader().isOpened()){									
			progressRead.show();
			pickDataCard = new CardDeviceTask(ConstantsValues.tarjeta,progressRead,
					ConstantsValues.MOUNT_PURCHASE,contexto);				
			try {
				dataEMV = pickDataCard.execute().get();
				//--DrawView
				if(dataEMV != null){
					if (dataEMV.getHoldersName().isEmpty()||dataEMV.getHoldersName().equals(null)){							
						compraReceived.setNombre("N/A");
					}else{
						compraReceived.setNombre(dataEMV.getHoldersName());
					}	
					compraReceived.setCard(dataEMV.getPAN());
					//compraReceived.setMarcaTarjeta(dataEMV.getTypeCard());
					compraReceived.setMoneda(dataEMV.getCountryIssuerCode());
					compraReceived.setVigencia(Utils.prosaDateFormated(dataEMV.getExpDate()));
					arqc = dataEMV.getARQC();	
					aid = dataEMV.getAID();
					Log.d(ConstantsValues.TAG_READER, "Colocando datos en vista...");					
					Log.d(ConstantsValues.TAG_READER, "ESTO VALE: "+compraReceived.getCard());
					holderName.setText(!(compraReceived.getNombre().isEmpty()) ? compraReceived.getNombre() :
						contexto.getString(R.string.card_holder_name_empty));		
					
				}
			} catch (InterruptedException e) {
				Log.e(ConstantsValues.TAG_READER, "INTERRUPTION EXCEPTION: "+e);					
			} catch (ExecutionException e) {
				Log.e(ConstantsValues.TAG_READER, "EXECUTION EXCEPTION: "+e);
			}																				
		}else{
			CustomToastNotification.customMessage(contexto.getString(R.string.alert_text_1),getBaseContext()).show();
			
			
		}
	}
	public void settingSendDevice(){
		item = (MenuItem) menu.findItem(R.id.action_send);
		buttonSend = (Button) item.getActionView().findViewById(R.id.buttonSend);	
		buttonSend.setBackgroundResource(R.drawable.blue_button_shape);
		buttonSend.setOnClickListener(new View.OnClickListener() {					
			@Override
			public void onClick(View v) {												
				
				if(Utils.internetAvailable(contexto)){
					confirmSell();
				}else{
					GenericAlert.generateAlertDWiFi(contexto.getString(R.string.app_name), 
							contexto.getString(R.string.alert_text_wifi), contexto).show();
				}
					
				}											
		});
	}
	
	public void settingSendSell(){
		if(!cvv2Number.getText().toString().isEmpty() || cvv2Number.getText() != null){
			Log.i(ConstantsValues.TAG_READER, "cardsend0: {" + cvv2Number.getText() +"}");
			compraReceived.setCvv2(cvv2Number.getText().toString());
			if(Validations.TAEPurchaseValidation(compraReceived, contexto)){
				//confirmacionCompra.setMessage(returnDetailPurchaseValues(isTae,dataEMV).toString());
				//confirmacionCompra.show();
				showDataOnLayout();
				settingSendDevice();
				Log.i(ConstantsValues.TAG_READER, "cardsend: {" + cvv2Number.getText().length() +"}");
			}else{
				CustomToastNotification.customMessage(contexto.getString(R.string.alert_text_2), getBaseContext()).show();

				Log.i(ConstantsValues.TAG_READER, "cardsend1: {" + compraReceived.getCard() +"}");
				Log.i(ConstantsValues.TAG_READER, "cardsend2: {" + compraReceived.getVigencia() +"}");
				Log.i(ConstantsValues.TAG_READER, "cardsend3: {" + compraReceived.getCvv2() +"}");
			}
		}else{
			card = new cardDialog(contexto, contexto.getString(R.string.label_paso_3),
					contexto.getString(R.string.label_msn_paso_3));
			card.setCancelable(false);
			card.requestWindowFeature(Window.FEATURE_NO_TITLE);
			card.setDialogResult(new DialogListener(){
				@Override
				public void finish(String CVV) {
					if(CVV.length() == 3){
						cvv2Number.setText(CVV);
						settingSendSell();
					}else{
						cvv2Number.setText(null);
				
					}
				}
			});
			card.show();
		}
		
	}
	

	public void showDataOnLayout(){		
		cardNumber.setText(Utils.cardNumberFormat(compraReceived.getCard()));						
		Log.d(ConstantsValues.TAG_READER, "ESTO VALE: "+compraReceived.getCard());
		Log.d(ConstantsValues.TAG_READER, "Monto: "+compraReceived.getMonto());
		//Log.d(ConstantsValues.TAG_READER, "AID: "+compraReceived.getCard());
		expiration.setText(compraReceived.getVigencia());				
		holderName.setText(!(compraReceived.getNombre().isEmpty()) ? compraReceived.getNombre() :
			contexto.getString(R.string.card_holder_name_empty));		
		fechaHora.setText(Utils.getdayDate().toString()+" "+Utils.getHourDate());
		if(compraReceived.getNumbertae().isEmpty()){
			tarjeta.setText(Utils.cardNumberFormat(compraReceived.getCard()));
			titular.setText(!(compraReceived.getNombre().isEmpty()) ? compraReceived.getNombre() :
				contexto.getString(R.string.card_holder_name_empty));
			monto.setText(Utils.setFloatFormat(Float.parseFloat(compraReceived.getMonto())));
			servicio.setText(ConstantsValues.LABEL_PAGO_GENERICO + " " + compraReceived.getProducto());			
			labelTAE.setVisibility(View.INVISIBLE);
			labelCompania.setVisibility(View.INVISIBLE);
			numeroTAE.setVisibility(View.INVISIBLE);
			compania.setVisibility(View.INVISIBLE);
		}else{
			tarjeta.setText(Utils.cardNumberFormat(compraReceived.getCard()));
			titular.setText(!(compraReceived.getNombre().isEmpty()) ? compraReceived.getNombre() :
				contexto.getString(R.string.card_holder_name_empty));
			monto.setText(compraReceived.getMonto());
			servicio.setText(ConstantsValues.RECARGA_TAE + " " +
			Utils.getCompanyLabel(compraReceived.getProveedor()));
			numeroTAE.setText(compraReceived.getNumbertae());
			compania.setText(Utils.getCompanyLabel(compraReceived.getProveedor()));
		}
		
		
	}
	
	
	public void confirmSell(){
		try{
			if(isTae){
				Log.i(ConstantsValues.TAG_READER, "objeto json" + jsonParser.toJson(compraReceived));
				Log.i(ConstantsValues.TAG_READER, "Objeto:" + Utils.getJSON(compraReceived));
				//new PayServicesTask(compraReceived,contexto,arqc).execute(Blowfish.encode(jsonParser.toJson(compraReceived)));
				new PayServicesTask(compraReceived, contexto, arqc, aid).execute(Blowfish.encode(Utils.getJSON(compraReceived)));
			}else{
				Log.d(ConstantsValues.TAG_READER,"PROCESANDO COBRO GENERICO SIMULADO...");
				transactionBusiness = new TransactionBusiness(getApplicationContext());											
				transactionBusiness.insertTransaction(compraReceived, isTae);
				Log.d(ConstantsValues.TAG_READER,"ubic" + compraReceived.getCx());
				
				CustomToastNotification.customMessage(contexto.getString(R.string.notification_transaction1),
						contexto).show();
																								
				Intent toSign = new Intent (contexto, FingerSignature.class);				
				receiver.putSerializable("completeSell", compraReceived);
				receiver.putString("arqc", arqc);
				receiver.putString("aid", aid);
				toSign.putExtras(receiver);
				startActivity(toSign);
			}					
		}catch(Exception e){
			Log.e(ConstantsValues.TAG_READER, "ERROR AL PROCESAR PAGO: ["+ e +"]");
			CustomToastNotification.customMessage(contexto.getString(R.string.notification_transaction_error1),
					getBaseContext()).show();
			Intent toSign = new Intent(getBaseContext(),MainMenuActivity.class);
			toSign.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(toSign);
		}
	}
}//end class
